<?php
namespace app\member\model;

use app\BaseModel;
use think\facade\Db;

class Growth extends BaseModel
{
    //会员权益表
    protected $name = 'longbing_card_member_growth';


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function growthAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 16:35
     * @功能说明:列表
     */
    public function growthList($dis,$page){

        $data = $this->where($dis)->order(['update_time desc','id desc'])->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if($v['controller_type']==0){

                    $v['controller_name'] = '系统核算';

                    $icon = $v['growth_add']>0?'+':'-';

                    $growth_add = $v['growth_add']>0?$v['growth_add']:$v['growth_add']*-1;

                    $refund = $v['refund']==1?'(已退款)':'';

                    $boj    = $this->returnObj($v['type']);

                    $v['text'] = $boj.'成长值'.$icon.$growth_add.',现成长值 '.$v['growth_after'].$refund;

                }else{

                    $v['controller_name'] = Db::name('longbing_admin')->where(['admin_id'=>$v['controller']])->value('account');

                    $v['text'] = '成长值'.$v['growth_before'].',修改为 '.$v['growth_after'];

                }

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-14 15:17
     * @功能说明:返回一个对象
     */
    public function returnObj($type){


        switch ($type){
            //购买会员商品
            case 0:
                return '购买会员获得';

                break;
            //会员购买商品
            case 1:
                return '消费获得';

                break;
            //购买储值套餐赠送
            case 2:
                return '购买储值套餐赠送';

                break;
            default:
                return '消费获得';

                break;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function growthInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function growthUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-28 16:29
     * @功能说明:
     */
    public function settledGrowth($uniacid,$user_id){

        $dis[] = ['uniacid','=',$uniacid];

        $dis[] = ['growth_add','>',0];

        $dis[] = ['is_del','=',0];

        $dis[] = ['status','=',1];

        $dis[] = ['user_id','=',$user_id];

        $dis[] = ['refund','=',0];

        $data = $this->where($dis)->sum('growth_add');

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 10:21
     * @功能说明:每日领取的积分
     */
    public function dayGrowth($user_id,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        if($config['growth_limit']==1){

            $dis = [

                'user_id' => $user_id,

                'refund'  => 0,

                'type'    => 1
            ];

            $growth    = $this->where($dis)->where('status','>',0)->whereDay('create_time')->sum('growth_add');

            $re_growth = $config['growth_day_max'] - $growth;

            $re_growth = $re_growth>0?$re_growth:0;

            return intval($re_growth);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 10:47
     * @功能说明:增加用户的成长值记录
     */
    public function addUserGrowth($uniacid,$user_id,$growth,$order_id = 0,$type=0,$status = 1){

        if(!empty(intval($growth))){

            $member_model = new Member();

            $member_info  = $member_model->getMemberInfo(['user_id'=>$user_id,'uniacid'=>$uniacid]);

            $insert = [

                'uniacid' => $uniacid,

                'user_id' => $user_id,

                'growth_add' => intval($growth),

                'growth_before' => intval($member_info['growth']),

                'growth_after'  => intval($member_info['growth']+$growth),

                'order_id'      => $order_id,

                'type'          => $type,

                'status'        => $status,

            ];

            $res = $this->growthAdd($insert);

            return $res;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 11:07
     * @功能说明:给用户加成长值
     */
    public function incUserGrowth($order_id,$user_id,$uniacid){

        $member_model = new Member();

        $dis = [

            'order_id' => $order_id,

            'status'   => 1
        ];

        $info = $this->where($dis)->select()->toArray();

        if(!empty($info)){

            foreach ($info as &$value){

                $member_info  = $member_model->getMemberInfo(['user_id'=>$user_id,'uniacid'=>$uniacid]);

                $growth = $value['growth_add'];

                $update = [
                    //修改状态
                    'status' => 2,
                    //当前
                    'growth_before' => intval($member_info['growth']),
                    //修改后对
                    'growth_after'  => intval($member_info['growth']+$growth),

                ];

                $this->growthUpdate(['id'=>$value['id']],$update);

                if(is_numeric($growth)&&$growth>0){

                    $dis = [

                        'user_id' => $user_id

                    ];

                    $member_model->incDecGrowth($dis,$growth,'growth');

                }

            }

        }

        return true;

    }













}